package com.bree.erp.jsf;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.bree.erp.entity.Project;
import com.bree.erp.entity.PurchaseRequisition;
import com.bree.erp.entity.User;
import com.bree.erp.jsf.httpsess.SessionDataKey;
import com.bree.erp.jsf.httpsess.UserSessionData;
import com.bree.erp.service.ProjectService;
import com.bree.erp.service.PurchaseRequisitionService;

@ManagedBean(name="createPurchaseRequisitionBean")
@RequestScoped
public class CreatePurchaseRequisitionBean {
	
	private static Logger LOG = Logger.getLogger(CreatePurchaseRequisitionBean.class);
	
	private Long projectId;
	private String description;
	
	@ManagedProperty(value="#{authUserBean}")
	private AuthUserBean authUserBean;
	
	@ManagedProperty(value="#{userSession}")
	private UserSessionData userSession;
	
	@EJB
	private ProjectService projectService;
	@EJB
	private PurchaseRequisitionService prService;
	
	public String create() {
		
		Project project = projectService.read(projectId);
		User user = authUserBean.getLoggedInUser();
		
		PurchaseRequisition pr = prService.create(user, project, this.getDescription());
		
		this.setProjectId(null);
		this.setDescription("");
		
		userSession.put(SessionDataKey.SELECTED_PURCHASE_REQUISITION_ID, pr.getId());
		
		return "edit";
	}
	
	public String edit() {
		
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
		 
		String strId = params.get("prId");
		long id = Long.parseLong(strId);
		
		LOG.debug("Edit submitted for PR: " + id);
		
		userSession.put(SessionDataKey.SELECTED_PURCHASE_REQUISITION_ID, id);
		
		return "edit";
	}
	
	public void delete(long id) {
		
		prService.delete(id);
	}
	
	public boolean renderDelete(long id) {
		
		PurchaseRequisition pr = prService.read(id);
		
		// TODO: only creator can delete request? 
		if(this.authUserBean.getLoggedInUser().equals(pr.getCreatedBy())) {
			return true;
		}
		
		return false;
	}
	
	public List<Project> getProjects() {
		
		List<Project> list = new ArrayList<Project>();
		list.addAll(projectService.list());
		
		return list;
		
	}
	
	/**
	 * @return the projectId
	 */
	public Long getProjectId() {
		return projectId;
	}
	/**
	 * @param projectId the projectId to set
	 */
	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/**
	 * @return the authUserBean
	 */
	public AuthUserBean getAuthUserBean() {
		return authUserBean;
	}
	/**
	 * @param authUserBean the authUserBean to set
	 */
	public void setAuthUserBean(AuthUserBean authUserBean) {
		this.authUserBean = authUserBean;
	}

	/**
	 * @return the userSession
	 */
	public UserSessionData getUserSession() {
		return userSession;
	}

	/**
	 * @param userSession the userSession to set
	 */
	public void setUserSession(UserSessionData userSession) {
		this.userSession = userSession;
	}
	
	

}
